package com.example.action;

import com.example.bean.User;
import com.example.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 获取用户名密码
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        Cookie[] cookies = req.getCookies();

        // 自动登录
        String auto = req.getParameter("autoLogin");
        if (auto != null) {
            Cookie autoLogin = new Cookie("autoLogin", username + "@" + password);
            autoLogin.setMaxAge(60 * 60);
            autoLogin.setPath("/");
            resp.addCookie(autoLogin);
        } else {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("autoLogin")){
                    Cookie autoLogin = new Cookie("autoLogin", "");
                    autoLogin.setMaxAge(0);
                    autoLogin.setPath("/");
                    resp.addCookie(autoLogin);
                }
            }
        }

        // 记住用户名
        String remember = req.getParameter("remember-me");
        if (remember != null) {
            Cookie rememberMe = new Cookie("remember-me", username);
            rememberMe.setMaxAge(60 * 60);
            rememberMe.setPath("/");
            resp.addCookie(rememberMe);
        } else {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("remember-me")){
                    Cookie rememberMe = new Cookie("remember-me", "");
                    rememberMe.setMaxAge(0);
                    rememberMe.setPath("/");
                    resp.addCookie(rememberMe);
                }
            }
        }

        // 封装
        UserService userService = new UserService();
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        System.out.println(user);

        int n = userService.selectUserByName(user);
        if (n == -1) {
            req.setAttribute("infomation", "用户不存在");
            req.setAttribute("url", "login.jsp");
            req.setAttribute("second", 5);
            req.getRequestDispatcher("tip.jsp").forward(req, resp);
        } else if (n == 0) {
            req.setAttribute("infomation", "密码错误");
            req.setAttribute("url", "login.jsp");
            req.setAttribute("second", 5);
            req.getRequestDispatcher("tip.jsp").forward(req, resp);
        } else {
            user.setId(userService.selectUserByUsername(user).getId());
            req.getSession().setAttribute("loginUser", user);
            resp.sendRedirect(req.getContextPath() + "/index");
        }
    }
}